
/**
* Process the model
*
* @param  string  srcModel
* @param  string  srcModelId
* @param  string  srcDate
* @param  string  srcRef
* @param  string  srcNo
* @param  string  serNumber
*
*/
function processModel(srcModel,
                      srcModelId,
                      srcDate,
                      srcRef,
                      srcNo,
                      serNumber){

  srcModel    = document.getElementsByName(srcModel);
  srcModelId  = document.getElementsByName(srcModelId);
  srcDate     = document.getElementsByName(srcDate);
  srcRef      = document.getElementsByName(srcModel);
  srcNo       = document.getElementsByName(srcNo);
  serNumber   = document.getElementsByName(serNumber);
  
  if(srcModel.length > 0){
    
    srcModel = srcModel[0];
  }
  
  if(srcModelId.length > 0){
    
    srcModelId = srcModelId[0];
  }
  
  if(srcDate.length > 0){
    
    srcDate = srcDate[0];
  }
  
  if(srcRef.length > 0){
    
    srcRef = srcRef[0];
  }
  
  if(srcNo.length > 0){
    
    srcNo = srcNo[0];
  }
  
  if(serNumber.length > 0){
    
    serNumber = serNumber[0];
  }
  
  var xmlHttp =	new XMLHttpRequest();
  
  var url 		= "/ecs/tocmenu_44/controllers/processModel.php" +"?sid="+Math.random();
	var params 	= "srcModel="    + srcModel   + 
	              "srcModelId="  + srcModelId +
	              "srcDate="     + srcDate    +
	              "srcNo="       + srcNo      +
	              "srcRef="      + srcRef     +
	              "serNumber="   + serNumber;
	
	xmlHttp.open("POST", url, true);
	
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	
	xmlHttp.onreadystatechange = function() {//Call a function when the state changes.
		
		//alert("readyState :" + xmlHttp.readyState + " and status :" + xmlHttp.status);
		
		if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
		  //alert(xmlHttp.responseText);
		  switch(xmlHttp.responseText){
		    
		    case _CONN_ERROR:
		      
		      alert("Db connection failed");
		      break;
		      
		    case _RET_NULL:
		      
		      alert("Error has been occured");
		      srcModelId.innerHTML = "ERROR";
		      
		      break;
		      
		    default:
		      
		      srcModelId.innerHTML = xmlHttp.responseText;
		  }
		}
	}
	
	xmlHttp.send(params);
}

/**
* Process the miniInput
* save all entry into DB 
* and fetch the jobId of this entry
*
* @param  string  srcObject
* @param  string  srcVarName
* @param  string  srcQuantity
* @param  integer srcDtlNo
* @param  integer srcSubNo
*
*/
function processMiniInput(srcObject,
                          srcVarName,
                          srcQuantity,             
                          srcDtlNo,
                          srcSubNo){
  
  var _CONN_ERROR         = "****";
  var _RET_NULL           = " ";
  var _NO_NUMBER          = "_NO_NUMBER";
  var _MANUAL_KEY_IN      = "_MANUAL_KEY_IN";
  var _DEFAULT_SEPARATOR  = "~^~";
  var _NOT_BALANCE        = "~**~";
  
  var srcInput = "";
  
  srcObject   = document.getElementsByName(srcObject);
  srcVarName  = opener.document.getElementsByName(srcVarName);
  srcQuantity = opener.document.getElementsByName(srcQuantity);
  
  if(srcObject.length > 0){
    
    var maxLoop = srcObject.length;
    
    for(idx = 0; idx < maxLoop; idx++){
      
      srcInput = srcInput + srcObject[idx].value;
    }
  }else{
    
    srcInput = srcObject.value;
  }
  
  if(srcVarName.length > 0){
    
    srcVarName  = srcVarName[0];
  }
  
  if(srcQuantity.length > 0){
    
    srcQuantity  = srcQuantity[0];
  }
  
  if(srcObject.value != ""){
    
    var xmlHttp =	new XMLHttpRequest();
    
    var url 		= "/ecs/tocmenu_44/controllers/processMiniInput.php" + "?sid="+Math.random();
    
    var params 	= "srcInput="     + srcInput          +
                  "&srcQuantity=" + srcQuantity.value +
                  "&DTL_NO="      + srcDtlNo          +
                  "&SUB_NO="      + srcSubNo; 
  	              
  	xmlHttp.open("POST", url, true);
  	
  	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  	xmlHttp.setRequestHeader("Content-length", params.length);
  	xmlHttp.setRequestHeader("Connection", "close");
  	
  	xmlHttp.onreadystatechange = function() {//Call a function when the state changes.
  		
  		if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
  		  
  		  //alert(xmlHttp.responseText);
  		  
  		  switch(xmlHttp.responseText){
  		    
  		    case _CONN_ERROR:
  		      
  		      alert("Error: Oracle Connection failed");
  		      break;
  		    
  		    case _RET_NULL:
  		      
  		      alert("Error: System has given up, calculation failed");
  		      break;
  	      
  	      case _NO_NUMBER:
  	      
  	        /* Do nothing */
  	        break;
          
          case _NOT_BALANCE:
          
            alert("Error: Entry is not equal to source Quantity");
            break;
          
          default:
                          
            srcVarName.value = _MANUAL_KEY_IN     + 
                               _DEFAULT_SEPARATOR + 
                               xmlHttp.responseText;
            
            /* window.close(); */
  		  }
  		}
  	}
  	
  	xmlHttp.send(params);
  }else{
    
    alert("Error: No Input");
  }
}

/**
* Check if this dropDown is inited
*
* @param  string  modelEnt    the source object name
*
*/
function getModel(modelEnt){
	
	var _GO = "GO";
	
	var xmlHttp =	new XMLHttpRequest();
	
	var url 		= "/ecs/tocmenu_44/controllers/getIsInitListModel.php" + "?sid="+Math.random();
	var params 	= "modelEnt=" + modelEnt;
	
	xmlHttp.open("POST", url, true);
	
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	
	xmlHttp.onreadystatechange = function() {
	  
		//alert("readyState :" + xmlHttp.readyState + " and status :" + xmlHttp.status);
		
		if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
		
			//alert("Responsed :" + xmlHttp.responseText);
		
			if(xmlHttp.responseText == _GO){
			  
			  modelEnt  = document.getElementsByName(modelEnt);
			  
			  if(modelEnt.length > 0){
			    
			    modelEnt = modelEnt[0];
			  }
			  
			  var url1 		= "/ecs/tocmenu_44/controllers/getListModel.php" + "?sid="+Math.random();
      	var params1	= "";
      	
      	xmlHttp.open("POST", url1, true);
      	
      	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      	xmlHttp.setRequestHeader("Content-length", params1.length);
      	xmlHttp.setRequestHeader("Connection", "close");
      	
      	xmlHttp.onreadystatechange = function() {
      	  
      		//alert("readyState :" + xmlHttp.readyState + " and status :" + xmlHttp.status);
      		
      		if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
      			
      			//alert("Responsed :" + xmlHttp.responseText);
      			
      			modelEnt.innerHTML = xmlHttp.responseText;
      		}
      	}
      	
      	xmlHttp.send(params);
			}
		}
	}
	xmlHttp.send(params);
}

/**
* This is a test Function
* to test if the include is success
*
* @param  void
*
*/
function sayHelloAjax(){
  
  alert("ajax.js is linked");
}